<template>
	<view class="u-form-item">
		<view class="itemTitle" style="display: flex;">
			<view style="width: 20rpx;">
				<text style="color: red;" v-if="required">*</text>
			</view>
			{{title}}	
		</view>
		<u-upload ref="uUpload1" :fileList="fileList" :header="header" :action="action" name="file" :multiple="true"
			:maxCount="10" :before-upload="beforeUpload" on-error="onError" @on-success="onSuccess"></u-upload>
	</view>
</template>

<script>
	export default {
		name: "uploadPhoto",
		props: ["title", "required", "action", "listName","fileList"],
		data() {
			return {
				header: {
					Accept: 'application/json, text/plain, */*',
					contentType:'application/json;charset=UTF-8',  //设置后
					// ContentType: 'multipart/form-data',
					Authorization: this.$store.state.vuex_token
				},
				// fileList: []
			};
		},
		methods: {
			beforeUpload(index, list) {
				return true
			},
			onError(res, index, lists, name) {
				console.log(res)
			},
			onSuccess(data, index, lists, name) {
				
				
				
				let newList = new Array();
				let listObj = this.$u.deepClone(lists)
				
				console.log(listObj)
				for (var i = 0; i < listObj.length; i++) {
					let response = listObj[i].response;
					
					if(response==undefined){
						console.log(i)
					}else{
						if (response.code == 200) {
							var item = {
								fileName:listObj[i].response.fileName,
								originalFilename:listObj[i].response.originalFilename,
								url:listObj[i].response.url,
								thumbFileUrl:listObj[i].response.thumbFileUrl,
								
							}	
							newList.push(JSON.stringify(item))
						}
					}
					

				}
				let totList = {
					listName: this.listName,
					title:this.listName,
					data: newList
				}
				
				console.log(totList)
				this.$emit('child_event', totList)
			},

		},
	}
</script>

<style>
.u-form-item {
			background-color: #fff;
			margin: 15rpx 25rpx 5rpx 25rpx;
			padding: 15rpx;
			border-radius: 3rpx;
			box-shadow: 3px 3px 3px #999;
			font-size: 16px;

			.itemTitle {
				padding: 25rpx 0;
				
			}
		}
</style>
